package handler

import (
	"apibe/grpc/rome/v1/factory"
	"apibe/grpc/rome/v1/service"
	"apibe/grpc/rome/v1/service/impl"
	"apibe/grpc/rome/v1/share/crypt"
	"context"
	"go.mongodb.org/mongo-driver/bson"
)

/*
	创建|修改
	根据传入的 _id 进行区分，当 _id 为nil时，创建
	当 _id 不为空时，修改
*/
func TaskUpsertHandler(task *impl.Task, claims *crypt.CustomClaims) error {
	_, err := task.UpsertOne(claims)
	return err
}

/*
	根据条件获取所有的 design (包含logger)
*/
func TaskFindManyHandler(filter map[string]interface{}, page int, pageSize int) (*[]impl.Task, int64, error) {
	task, err := service.NewService(service.TaskService)
	if err != nil {
		return nil, 0, err
	}
	filter["status"] = bson.M{"$lt": factory.Deleted}
	cursor, count, err := task.FindManyFilter(filter, page, pageSize)
	if err != nil {
		return nil, 0, err
	}
	var ds []impl.Task
	err = cursor.All(context.Background(), &ds)
	if err != nil {
		return nil, 0, err
	}
	return &ds, count, err
}
